package de.gwtprime.shared;

import java.io.Serializable;
import java.util.Date;

import com.google.gwt.user.client.rpc.IsSerializable;


/**
 * Represents a trading position
 * @author jan
 *
 */
public abstract class TradingPosition  implements Serializable, IsSerializable, Comparable<TradingPosition>{

	
	/**
	 * Serial UID
	 */
	private static final long serialVersionUID = 5685634719697265264L;
	
	/**
	 * Date when the position was opened.
	 */
	private Date openDate;
	
	/**
	 * Date when the position was closed.
	 */
	private Date closeDate;
	
	
	/**
	 * Position was opened for this value.
	 */
	private double openValue;
	
	/**
	 * Position was closed for this value.
	 */
	private double closeValue;
	
	
	/**
	 * Save information which strat closed the position
	 */
	private String closingStrat;
	
	/**
	 * Save information which strat opened the position
	 */
	private String openStrat;
	
	public Date getOpenDate() {
		return openDate;
	}
	public void setOpenDate(Date openDate) {
		this.openDate = openDate;
	}
	public Date getCloseDate() {
		return closeDate;
	}
	public void setCloseDate(Date closeDate) {
		this.closeDate = closeDate;
	}
	public double getOpenValue() {
		return openValue;
	}
	public void setOpenValue(double openValue) {
		this.openValue = openValue;
	}
	public double getCloseValue() {
		return closeValue;
	}
	public void setCloseValue(double closeValue) {
		this.closeValue = closeValue;
	}
	
	
	public abstract double getProfit();
	
	/**
	 * Get percentual profit
	 * @return
	 */
	public double getProfitPer(){
		return getProfit()/getOpenValue()*100;
	}
	
	/**
	 * Sort by date
	 */
	@Override
	public int compareTo(TradingPosition o) {
		return openDate.compareTo(o.getOpenDate());
	}
	
	
	/**
	 * Get time that position was hold in days
	 * @return time in days
	 */
	public long getHoldTime(){
		
		Date closeDate = SharedHelper.setTimeToZero(getCloseDate());
		Date openDate = SharedHelper.setTimeToZero(getOpenDate());
		
		return (closeDate.getTime() - openDate.getTime())/1000/60/60/24;
	}
	
	public String getClosingStrat() {
		return closingStrat;
	}
	
	public void setClosingStrat(String closingStrat) {
		this.closingStrat = closingStrat;
	}
	
	public abstract String getType();
	public String getOpenStrat() {
		return openStrat;
	}
	public void setOpenStrat(String openStrat) {
		this.openStrat = openStrat;
	}
	
	
	/**
	 * Type of Trading positions -> short and long
	 */
	public enum TradingPositionType{
		LONG,
		SHORT
	}
	
	/**
	 * Strategies that open positions.
	 */
	public enum OpenStratType{
		KELTNERKANAL,
		TURN_AROUND
	}
	
	public abstract TradingPositionType getTradingType();
	
	public OpenStratType getOpenStratType() {
		return openStratType;
	}
	public void setOpenStratType(OpenStratType openStratType) {
		this.openStratType = openStratType;
	}

	private OpenStratType openStratType = null;
	
	
}
